﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace WSBackup
{
    public partial class FormRevisions : Form
    {
        private readonly WSSettingsXml settings;
        private readonly string BackupFileName;
        private readonly string CurrentFileName;
        private FileArchiver fa = null;

        public FormRevisions(WSSettingsXml settings, string CurrentFolder, string BackupFileName)
        {
            InitializeComponent();
            this.BackupFileName = BackupFileName;
            this.CurrentFileName = CurrentFolder + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(BackupFileName);
            this.settings = settings;

        }

        private string FileName { get { return Path.GetFileNameWithoutExtension(BackupFileName); } }

        private bool Progress(string message)
        {
            this.Text = message; // String.Format("{0:P0}", (float)inSize / (float)inSizeTotal);
            Application.DoEvents();
            return true;
        }

        private void FormRevisions_Shown(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;

            fa = new FileArchiver(BackupFileName, Progress);

            toolStripStatusLabel1.Text = "Compression: " + (fa.CompresionMethod == CompresionMethod.SevenZip ? "7zip" : "Simple");

            for (var i = 0; i < fa.Revisions.Length; i++)
            {
                var rev = fa.Revisions[i];
                ListViewItem lvi = new ListViewItem((i + 1).ToString());
                lvi.Tag = i;
                lvi.SubItems.Add(rev.DateTime.ToString("dd.MM.yyyy HH:mm:ss"));
                lvi.SubItems.Add(rev.Size.ToString("#,##0"));
                listView1.Items.Add(lvi);
            }

            this.Text = "List of revisions " + BackupFileName;
            Cursor = Cursors.Default;

        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 1)
            {
                var path = Path.Combine(Path.GetTempPath(), FileName);
                using (var fs = fa.GetRevision((int)listView1.SelectedItems[0].Tag, path))
                {
                    fs.Close();
                }
                Process.Start(path);
            }
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            openToolStripMenuItem.Enabled = listView1.SelectedItems.Count == 1;
            compareToolStripMenuItem.Enabled = listView1.SelectedItems.Count == 2;
        }

        private void compareToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var fileNameFormat = Path.GetTempPath() + Path.GetFileNameWithoutExtension(FileName) + ".{0}" + Path.GetExtension(FileName);
            var path1 = String.Format(fileNameFormat, listView1.SelectedItems[0].Tag);
            fa.GetRevision((int)listView1.SelectedItems[0].Tag, path1).Close();
            var path2 = String.Format(fileNameFormat, listView1.SelectedItems[1].Tag);
            fa.GetRevision((int)listView1.SelectedItems[1].Tag, path2).Close();
            var psi = new ProcessStartInfo(settings.Comparer);
            psi.Arguments = string.Format("\"{0}\" \"{1}\"", path1, path2);
            Process.Start(psi);
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var fileNameFormat = Path.GetTempPath() + Path.GetFileNameWithoutExtension(FileName) + ".{0}" + Path.GetExtension(FileName);
            var path1 = String.Format(fileNameFormat, listView1.SelectedItems[0].Tag);
            fa.GetRevision((int)listView1.SelectedItems[0].Tag, path1).Close();
            Process.Start(path1);
        }

        private void FormRevisions_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (fa != null)
                fa.Dispose();
        }

        private void extractToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog1.FileName = this.CurrentFileName;
            if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                fa.GetRevision((int)listView1.SelectedItems[0].Tag, saveFileDialog1.FileName).Close();
            }
        }

    }
}
